package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ClipboardUtil;
import com.yuwj.chinaa.util.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

//资产收款界面
public class AssetsReceiveActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_save_qrcode)
    TextView tvSaveQrcode;
    @BindView(R.id.tv_qrcode)
    TextView tvQrcode;
    @BindView(R.id.ll_copy)
    LinearLayout llCopy;
    IPresenter presenter;
    Bitmap bitmapWithQRCode;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_assets_receive;
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, AssetsReceiveActivity.class);
        return intent;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {
        tvTitle.setText("收款");
        tvQrcode.setText(entityUser.getBurseQrCode());
        bitmapWithQRCode = CodeUtils.createImage(entityUser.getBurseQrCode(), 400, 400, null);
        if (bitmapWithQRCode != null) {
            ivQrcode.setImageBitmap(bitmapWithQRCode);
        } else {
            ToastUtil.getInstance().shortLength(context, "二维码生成失败");
        }
    }

    @Override
    public void initOperator() {

    }

    @OnClick({R.id.iv_back, R.id.tv_save_qrcode, R.id.ll_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_save_qrcode:
                if (bitmapWithQRCode != null) {
                    //首先创建下载目录
                    String path = ConstantParam.DOWNLOAD_FILE_PATH;
                    File dir = new File(path);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    //下载目录下的收款图片地址
                    String filePic = ConstantParam.DOWNLOAD_FILE_PATH + "充值地址.jpg";
                    try {
                        FileOutputStream fos = new FileOutputStream(filePic);
                        bitmapWithQRCode.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                        ToastUtil.getInstance().shortLength(context, "文件已保存到" + filePic);
                    } catch (IOException e) {
                        e.printStackTrace();
                        ToastUtil.getInstance().shortLength(context, "文件保存失败");
                    }
                }
                break;
            case R.id.ll_copy:
                String inviteCode = tvQrcode.getText().toString();
                ClipboardUtil.copyText(context, inviteCode);
                ToastUtil.getInstance().shortLength(context, "复制成功");
                break;
        }
    }
}
